package com.mipo.company.flytek.cycore.api.beilun;

import java.lang.reflect.Constructor;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.iflytek.cycore.usercenter.api.AppAccessSvc;
import com.iflytek.cycore.usercenter.api.AreaInfoSvc;
import com.iflytek.cycore.usercenter.api.ClassSvc;
import com.iflytek.cycore.usercenter.api.CommonSvc;
import com.iflytek.cycore.usercenter.api.EduorgSvc;
import com.iflytek.cycore.usercenter.api.RightInfoSvc;
import com.iflytek.cycore.usercenter.api.RoleSvc;
import com.iflytek.cycore.usercenter.api.SchoolSvc;
import com.iflytek.cycore.usercenter.api.StatisticsSvc;
import com.iflytek.cycore.usercenter.api.UserInfoSvc;

/**
 * 客户端工厂类
 * 
 * @author yuwang
 *         <p>
 *         Modify by jfyang 2014-02-10
 *         </p>
 * @version 1.0.5
 * 
 *          2014-02-10
 *          
	this.appkey = "0f739381132c449f8e49e82912d35cf3";
	this.appName = "rrt_nbbl";
	this.version = "4.32.16.1067";
	this.url = "http://usercenter.beiluntest.changyan.cn/usercenter";
 */

public class CyClientFactory {
    private static Logger log = LoggerFactory.getLogger(CyClientFactory.class);
    private static final HttpClient HTTP_CLIENT = getHttpClient();

    private static final String DEFAULT_APPKEY = "de88cc8705a521294313328344dddf";
    private static final String DEFAULT_APPNAE = "rrt_cqyb";
    private static final String DEFAULT_VERSION = "1.0.4";
    private static final String DEFAULT_URL = "http://172.16.16.19:50301/usercenter/";

    private String appkey;

    private String appName;

    private String version;

    private String url;

    private static HttpClient getHttpClient() {
        return getHttpClientBuilder().build();
    }

    public static HttpClientBuilder getHttpClientBuilder() {
        return HttpClientBuilder.create().setMaxConnTotal(Config.getMaxTotalConnect())
                .setMaxConnPerRoute(Config.getMaxConnectPerRoute());
    }

    /**
     * 初始化cycore client
     * 
     * @param clszz
     * @param serverUrl
     * @return
     */
    protected <T> T createClient(Class<T> clszz, String serverUrl) {
        try {
            THttpClient httpClient = new THttpClient(serverUrl, HTTP_CLIENT);
            httpClient.setCustomHeader("AppName", this.getAppName());
            httpClient.setCustomHeader("AppSecret", this.getAppkey());
            httpClient.setCustomHeader("ClientType", "Java");
            httpClient.setCustomHeader("ClientVersion", this.getVersion());

            TProtocol lopFactory = new TBinaryProtocol(httpClient);
            Constructor<T> construct = clszz.getConstructor(TProtocol.class);
            return construct.newInstance(lopFactory);

        } catch (Exception ex) {
            log.error("thrift client init excepiton:", ex);
            return null;
        }
    }

    /**
     * 获取UserInfoSvc.Iface
     * 
     * @return
     */
    public UserInfoSvc.Iface createUserSvc() {
        String serverUrl = String.format("%s/user_info", this.getUrl());
        System.out.println(serverUrl);
        return createClient(UserInfoSvc.Client.class, serverUrl);
    }

    /**
     * 获取SchoolSvc.Iface
     * 
     * @return
     */
    public SchoolSvc.Iface createSchoolSvc() {
        String serverUrl = String.format("%s/school_info", this.getUrl());
        return createClient(SchoolSvc.Client.class, serverUrl);
    }

    /**
     * 获取ClassSvc.Iface
     * 
     * @return
     */
    public ClassSvc.Iface createClassSvc() {
        String serverUrl = String.format("%s/class_info", this.getUrl());
        return createClient(ClassSvc.Client.class, serverUrl);
    }

    /**
     * 获取AreaInfoSvc.Iface
     * 
     * @return
     */
    public AreaInfoSvc.Iface createAreaSvc() {
        String serverUrl = String.format("%s/area_info", this.getUrl());
        return createClient(AreaInfoSvc.Client.class, serverUrl);
    }

    /**
     * 获取RoleSvc.Iface
     * 
     * @return
     */
    public RoleSvc.Iface createRoleSvc() {
        String serverUrl = String.format("%s/role_info", this.getUrl());
        return createClient(RoleSvc.Client.class, serverUrl);
    }

    /**
     * 获取CommonSvc.Iface
     * 
     * @return
     */
    public CommonSvc.Iface createCommonSvc() {
        String serverUrl = String.format("%s/common", this.getUrl());
        return createClient(CommonSvc.Client.class, serverUrl);
    }

    /**
     * 获取StatisticsSvc.Iface
     * 
     * @return
     */
    public StatisticsSvc.Iface createStatisticsSvc() {
        String serverUrl = String.format("%s/statistics/", this.getUrl());
        return createClient(StatisticsSvc.Client.class, serverUrl);
    }

    /**
     * 获取EduorgSvc.Iface
     * 
     * @return
     */
    public EduorgSvc.Iface createEduorgSvc() {
        String serverUrl = String.format("%s/eduorg_info", this.getUrl());
        return createClient(EduorgSvc.Client.class, serverUrl);
    }

    /**
     * 获取AppAccessSvc.Iface
     * 
     * @return
     */
    public AppAccessSvc.Iface createAppAccessSvc() {
        String serverUrl = String.format("%s/app_access", this.getUrl());
        return createClient(AppAccessSvc.Client.class, serverUrl);
    }

    /**
     * 获取RightInfoSvc.Iface
     * 
     * @return
     */
    public RightInfoSvc.Iface createRightInfoSvc() {
        String serverUrl = String.format("%s/right_info", this.getUrl());
        return createClient(RightInfoSvc.Client.class, serverUrl);

    }

    public String getAppkey() {
        if (StringUtils.isEmpty(appkey)) {
            this.appkey = "0f739381132c449f8e49e82912d35cf3";
        }
        if (StringUtils.isEmpty(appkey)) {
            return DEFAULT_APPKEY;
        }
        return appkey;
    }

    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }

    public String getAppName() {
        if (StringUtils.isEmpty(appName)) {
            this.appName = "rrt_nbbl";
        }
        if (StringUtils.isEmpty(appName)) {
            return DEFAULT_APPNAE;
        }
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getVersion() {
        if (StringUtils.isEmpty(version)) {
            this.version = "4.32.16.1067";
        }
        if (StringUtils.isEmpty(version)) {
            return DEFAULT_VERSION;
        }
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getUrl() {
        if (StringUtils.isEmpty(url)) {
            this.url = "http://usercenter.beiluntest.changyan.cn/usercenter";
        }
        if (StringUtils.isEmpty(url)) {
            return DEFAULT_URL;
        }
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }



    public static class Config {
        private static int maxTotalConnect = 20;
        private static int maxConnectPerRoute = 10;

        public static int getMaxTotalConnect() {
            return maxTotalConnect;
        }

        public static void setMaxTotalConnect(int maxTotalConnect) {
            Config.maxTotalConnect = maxTotalConnect;
        }

        public static int getMaxConnectPerRoute() {
            return maxConnectPerRoute;
        }

        public static void setMaxConnectPerRoute(int maxConnectPerRoute) {
            Config.maxConnectPerRoute = maxConnectPerRoute;
        }
    }
}
